package com.duolebo.uteped_sdk;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.duolebo.uteped_sdk.utils.Const;
import com.duolebo.uteped_sdk.utils.DeviceHelper;
import com.yc.nadalsdk.bean.MessageInfo;
import com.yc.nadalsdk.bean.Notify;
import com.yc.nadalsdk.bean.TimeClock;
import com.yc.nadalsdk.ble.open.UteBleClient;
import com.yc.nadalsdk.ble.open.UteBleConnection;
import com.yc.nadalsdk.ble.open.UteBleDevice;
import com.yc.nadalsdk.listener.BleConnectStateListener;
import com.yc.nadalsdk.listener.DeviceNotifyListener;

import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

/**
 * @Author: LiuYang
 * @Date: 2023/12/20 10:58
 * @Description:
 */
public class UTEPedBleClientHelper implements DeviceNotifyListener, BleConnectStateListener {

    private final static String TAG = "UTEPedBleClientHelper";

    private static UTEPedBleClientHelper instance;
    private final UteBleClient uteBleClient;
    private final UteBleConnection uteBleConnection;
    private final List<DeviceNotifyListener> deviceNotifyListeners = new ArrayList<>();
    private final List<BleConnectStateListener> connectStateListeners = new ArrayList<>();
    private final List<LocalNotifyListener> localNotifyListeners = new ArrayList<>();

    public static UTEPedBleClientHelper getInstance(Context context) {
        if (instance == null) {
            instance = new UTEPedBleClientHelper(context);
        }
        return instance;
    }

    public static UTEPedBleClientHelper shareInstance() {
        return instance;
    }

    public UTEPedBleClientHelper(Context context) {
        this.uteBleClient = UteBleClient.initialize(context.getApplicationContext());
        this.uteBleConnection = this.uteBleClient.getUteBleConnection();
        this.uteBleConnection.setDeviceNotifyListener(this);
        this.uteBleConnection.setConnectStateListener(this);
        //是否支持用户Id绑定
        this.uteBleClient.setSupportUserIdPair(true);
    }

    public UteBleClient getUteBleClient() {
        return this.uteBleClient;
    }

    public UteBleConnection getUteBleConnection() {
        return uteBleConnection;
    }

    public void sendMessage(MessageInfo messageInfo) {
        this.uteBleConnection.sendMessage(messageInfo);
    }

    public void addDeviceNotifyListener(DeviceNotifyListener listener) {
        if (!this.deviceNotifyListeners.contains(listener)) {
            this.deviceNotifyListeners.add(listener);
        }
    }

    public void removeDeviceNotifyListener(DeviceNotifyListener listener) {
        this.deviceNotifyListeners.remove(listener);
    }

    public void addBleConnectStateListener(BleConnectStateListener listener) {
        if (!this.connectStateListeners.contains(listener)) {
            this.connectStateListeners.add(listener);
        }
    }

    public void removeBleConnectStateListener(BleConnectStateListener listener) {
        this.connectStateListeners.remove(listener);
    }

    public void addLocalNotifyListener(LocalNotifyListener listener) {
        if (!this.localNotifyListeners.contains(listener)) {
            this.localNotifyListeners.add(listener);
        }
    }

    public void removeLocalNotifyListeners(LocalNotifyListener listener) {
        this.localNotifyListeners.remove(listener);
    }

    public void syncTime() {
        if (!this.uteBleClient.isConnected()) {
            Log.d(TAG, "no device connected.");
            return;
        }
        boolean enable = DeviceHelper.getInstance().read(Const.M_setTimeSyncEnable, false);
        if (!enable) {
            Log.d(TAG, "sync time not enabled");
            return;
        }

        TimeClock timeClock = new TimeClock();
        timeClock.setTimeSeconds((int) (System.currentTimeMillis()/1000));
        String displayName = TimeZone.getDefault().getDisplayName();
        Log.d(TAG, "syncTime:" + displayName);
        timeClock.setTimeZone(8);
        this.uteBleConnection.setTimeClock(timeClock);
    }

    @Override
    public void onNotify(@NonNull UteBleDevice uteBleDevice, @NonNull Notify notify) {
        try {
            for (DeviceNotifyListener listener: deviceNotifyListeners) {
                listener.onNotify(uteBleDevice, notify);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void release() {
        this.deviceNotifyListeners.clear();
        this.connectStateListeners.clear();
        this.localNotifyListeners.clear();
    }

    @Override
    public void onConnecteStateChange(int status) {
        for (BleConnectStateListener listener: connectStateListeners) {
            listener.onConnecteStateChange(status);
        }
    }

}
